#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#




import wsgiref.handlers
import os

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template

from helpal_models import HelpalUser
from helpal_models import Question



class MainHandler(webapp.RequestHandler):

  def get(self):
	user = users.get_current_user()
	
	#if user:
	#	self.response.headers['Content-Type'] = 'text/html'
	#	self.response.out.write('Hello, ' + user.nickname() + " (<a href='"+ users.create_logout_url(self.request.uri) +"'>Logout</a>)<br/><br/>")
	#else:
#		self.redirect(users.create_login_url(self.request.uri))
	# Instead Of Doing it this way
	# questions = db.GqlQuery("SELECT * FROM Question ORDER BY when DESC LIMIT 10")
	# Preferable to do it this way
	
	questions = Question.gql("ORDER BY when DESC LIMIT 10")
	#for question in questions:
	#	self.response.out.write("<h3>"+cgi.escape(question.title)+" <span style='font-size:12px;'> By "+question.author.user.nickname()+" @ "+ str(question.when) +"</span></h3>" + "<small>"+ cgi.escape(question.description) +"</small><br/>")
	
	template_values = {
		'title' : 'Latest 10 Questions',
		'questions' : questions,
		
	}
	#if users.is_current_user_admin():
	#	self.response.out.write("<br/><br/><a href='/admin/Ask'>Ask New Question</a>")
	path = os.path.join(os.path.dirname(__file__), 'main.html')
	self.response.out.write(template.render(path, template_values))



def main():
  application = webapp.WSGIApplication([('/', MainHandler)],debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
